package com.gimi.cloud.jimi.manage.service.prod.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.MyBeanUtils;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.commonService.common.JmOperationLogService;
import com.gimi.cloud.jimi.dao.dao.prod.ProdCatModelDao;
import com.gimi.cloud.jimi.dao.dao.prod.ProdCatModelValueDao;
import com.gimi.cloud.jimi.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import com.gimi.cloud.jimi.dto.prod.EcProdCatModelValueDTO;
import com.gimi.cloud.jimi.dto.product.ProdCatModelValueDTO;
import com.gimi.cloud.jimi.entity.prod.ProdCatModelEntity;
import com.gimi.cloud.jimi.entity.prod.ProdCatModelValueEntity;
import com.gimi.cloud.jimi.manage.service.prod.ProdCatModelValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @auther loongs
 * @date 2018/1/3  11:18
 */
@Service
public class ProdCatModelValueServiceImpl implements ProdCatModelValueService {
    private ProdCatModelValueDao prodCatModelValueDao;
    private ProdCatModelDao prodCatModelDao;
    private JmOperationLogService jmOperationLogService;

    @Autowired
    public ProdCatModelValueServiceImpl(ProdCatModelValueDao prodCatModelValueDao, ProdCatModelDao prodCatModelDao,JmOperationLogService jmOperationLogService) {
        this.prodCatModelValueDao = prodCatModelValueDao;
        this.prodCatModelDao=prodCatModelDao;
        this.jmOperationLogService=jmOperationLogService;
    }

    @Override
    public List<ProdCatModelValueEntity> getProdCatModelValueList(ProdCatModelValueDTO prodCatModelValueDTO) {
        Example example=new Example(ProdCatModelValueEntity.class);
        example.createCriteria().andCondition("model_id=",prodCatModelValueDTO.getModelId());
        List<ProdCatModelValueEntity> list=prodCatModelValueDao.selectByExample(example);
        return list;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean addProdCatModelValue(ProdCatModelValueDTO prodCatModelValueDTO) {

        //判断是否能删除、禁用或者启用、编辑
        ProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(prodCatModelValueDTO.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"添加失败，该品类模板值已上架，请确认后重试！");
        }

        //判断模板是否为空
        ProdCatModelEntity prodCatModelEntity=prodCatModelDao.selectByPrimaryKey(prodCatModelValueDTO.getModelId());
        if(prodCatModelEntity==null){
            throw new MsgException(ResponseCodeEnum.PRODCATID_IS_NOT_EXIT);
        }
        Example example =new Example(ProdCatModelValueEntity.class);
        example.createCriteria().andCondition("model_id=",prodCatModelValueDTO.getModelId()).
                andCondition("model_value=",prodCatModelValueDTO.getModelValue());
        List<ProdCatModelValueEntity> listTemp=prodCatModelValueDao.selectByExample(example);
        //判断添加的值是否存在
        if(listTemp.size() > 0){
              throw new MsgException(ResponseCodeEnum.PRODCATMODEL_VALUE_ID_FAIL);
        }
        ProdCatModelValueEntity prodCatModelValueEntity=new ProdCatModelValueEntity();
        BeanUtils.copyProperties(prodCatModelValueDTO,prodCatModelValueEntity);
        Integer flag=prodCatModelValueDao.insertSelective(prodCatModelValueEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(prodCatModelValueDTO,baseDTO);
            jmOperationLogService.insertAddLog(baseDTO,prodCatModelValueDTO.getCmId()+"","addProdCatModelValue",
                    "ec_prod_cat_model_value","添加指定品类模板属性值");
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public EcProdCatModelValueDTO saveOrUpdate(EcProdCatModelValueDTO dto) {
        ProdCatModelValueEntity prodCatModelValueEntity = null;
        if(null != dto.getCmId()){
            prodCatModelValueEntity = prodCatModelValueDao.selectByPrimaryKey(dto.getCmId());
        }else{
            prodCatModelValueEntity = new ProdCatModelValueEntity();
        }
        MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,prodCatModelValueEntity);
        //判断是否能删除、禁用或者启用、编辑
        ProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(prodCatModelValueEntity.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"保存失败，该品类模板值已上架，请确认后重试！");
        }
        int count = prodCatModelValueDao.selectCountByModelIdAndModelValueExcludeCmId(prodCatModelValueEntity.getModelId(),prodCatModelValueEntity.getModelValue(),prodCatModelValueEntity.getCmId());
        if(count > 0){
            throw new MsgException(ResponseCodeEnum.FAILURE,"保存失败，该品类模板值已存在，请确认后重试！");
        }
        if(null != prodCatModelValueEntity.getCmId()){
            prodCatModelValueDao.baseUpdateByPrimaryKey(prodCatModelValueEntity);
            jmOperationLogService.insertUpdateLog(dto,"saveOrUpdate","ec_prod_cat_model_value",
                    dto.getCmId()+"","编辑指定品类模板值");
        }else{
            prodCatModelValueDao.baseInsert(prodCatModelValueEntity);
            jmOperationLogService.insertAddLog(dto,dto.getCmId()+"","saveOrUpdate",
                    "ec_prod_cat_model_value","添加指定品类模板属性值");
        }
        MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(prodCatModelValueEntity,dto);
        return dto;
    }
    @Override
    public Integer deleteCatModelValueByCatIdExcludeCmIds(BaseDTO dto, String catId, List<Long> cmIds)   {
        if(StringUtil.isBlank(catId)){
            throw new MsgException(ResponseCodeEnum.PARAMETER_EXCEPTION,"删除模版值失败，传入品类id为空");
        }
        String sql = "UPDATE ec_prod_cat_model_value SET is_delete = 1,update_uid = '"+dto.getUpdateUid()+ "',update_uname ='"+dto.getUpdateUname()+"' ,update_time = NOW() " +
                "  WHERE is_delete = 0 AND model_id in (SELECT model_id FROM ec_prod_cat_model WHERE is_del = 1 AND prod_cat_id ='"+catId+"')";
        if(null != cmIds && cmIds.size() > 0){
            String cmIdStr = "";
            for (Long cmId:cmIds){
                cmIdStr += cmId+",";
            }
            if(cmIdStr.length() > 1){
                cmIdStr= cmIdStr.substring(0,cmIdStr.length() -1);
            }
            sql+=" AND cm_id NOT in("+cmIdStr+")";
        }
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        return prodCatModelValueDao.updateSql(queryParam);
    }

    @Override
    public ProdCatModelValueEntity gotoEditProdCatModelValue(ProdCatModelValueDTO prodCatModelValueDTO) {
        ProdCatModelValueEntity prodCatModelValueEntity= prodCatModelValueDao.selectByPrimaryKey(prodCatModelValueDTO.getCmId());
        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(),"gotoEditProdCatModelValue","ec_prod_cat_model_value",
                prodCatModelValueDTO.getCmId()+"","转跳到指定品类模板值得编辑界面");
        return prodCatModelValueEntity;
    }



    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean editProdCatModelValue(ProdCatModelValueDTO prodCatModelValueDTO) {

        //判断是否能删除、禁用或者启用、编辑
        ProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(prodCatModelValueDTO.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"编辑失败，该品类模板值已上架，请确认后重试！");
        }

        Example example =new Example(ProdCatModelValueEntity.class);
        example.createCriteria().andCondition("model_id=",prodCatModelValueDTO.getModelId()).
                andCondition("model_value=",prodCatModelValueDTO.getModelValue());
        List<ProdCatModelValueEntity> listTemp=prodCatModelValueDao.selectByExample(example);
        //判断添加的值是否存在,且不等于自身
        if(listTemp.size() > 0 && listTemp.get(0).getCmId().intValue()!=prodCatModelValueDTO.getCmId().intValue()){
            throw new MsgException(ResponseCodeEnum.PRODCATMODEL_VALUE_IS_EXIT_WHEN_EDIT);
        }

        ProdCatModelValueEntity prodCatModelValueEntity=new ProdCatModelValueEntity();
        BeanUtils.copyProperties(prodCatModelValueDTO,prodCatModelValueEntity);
        Integer flag=prodCatModelValueDao.updateByPrimaryKey(prodCatModelValueEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(prodCatModelValueDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"editProdCatModelValue","ec_prod_cat_model_value",
                    prodCatModelValueDTO.getCmId()+"","编辑指定品类模板值");
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean realDeleteProdCatModelValue(ProdCatModelValueDTO prodCatModelValueDTO) {
        ProdCatModelValueEntity prodCatModelValueEntity= prodCatModelValueDao.selectByPrimaryKey(prodCatModelValueDTO.getCmId());
        if(prodCatModelValueEntity == null){
            throw new MsgException(ResponseCodeEnum.PRODCATMODEL_VALUE_IS_NOT_EXIT);
        }

        //通过cmid查询model
        ProdCatModelEntity TempEntity=prodCatModelDao.selectModelByCmId(prodCatModelValueDTO.getCmId());
        //判断是否能删除、禁用或者启用、编辑
        ProdCatModelEntity isPublishedEntity = prodCatModelDao.selectProductIsPublished(TempEntity.getModelId());
        if(isPublishedEntity != null){
            throw new MsgException(ResponseCodeEnum.FAILURE,"删除失败，该品类模板值已上架，请确认后重试！");
        }
        Integer flag= prodCatModelValueDao.deleteByPrimaryKey(prodCatModelValueDTO.getCmId());
        if(flag>0){
            //添加操作日志
            jmOperationLogService.insertDeleteLog(prodCatModelValueDTO.getAddUid(),"realDeleteProdCatModelValue","ec_prod_cat_model_value",
                    prodCatModelValueDTO.getCmId()+"","删除指定的品类模板值");
            return true;
        }
        return false;
    }
}
